package com.example.noval.ui

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.noval.R
import com.example.noval.databinding.ActivityMainBinding
import com.example.noval.ui.adapter.ViewPager2Adapter
import com.example.noval.ui.page.BookMyFragment
import com.example.noval.ui.page.BookSearchFragment
import com.example.noval.ui.page.BookShelfFragment

class MainActivity : OnClickListener,AppCompatActivity() {
    
    private var viewpager2: ViewPager2? =null
    private var fragments:MutableList<Fragment> = mutableListOf()
    private var viewPager2Adapter:ViewPager2Adapter?=null
    private var binding:ActivityMainBinding ?= null
  

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        binding=DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
        viewpager2=binding?.viewpager2;
        loadPage()
        viewPager2Adapter= ViewPager2Adapter(supportFragmentManager,lifecycle, fragments)
        viewpager2?.adapter=viewPager2Adapter
        binding?.bookShelf?.setOnClickListener(this)
        binding?.bookSearch?.setOnClickListener(this)
        binding?.bookMy?.setOnClickListener(this)
        
    }
    

    override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
      
        
        return super.onCreateView(name, context, attrs)
    }

    private fun loadPage() {

        fragments.add(0,BookShelfFragment())
        fragments.add(1,BookSearchFragment())
        fragments.add(2,BookMyFragment())
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.book_shelf->{
                Log.d("zzl","点击了book_shelf")
                viewpager2?.setCurrentItem(0)
            }
            R.id.book_search->{
                viewpager2?.setCurrentItem(1)
            }
            R.id.book_my->{
                viewpager2?.setCurrentItem(2)
            }
        
        }
        
        

    }


}